#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：1.windows系统下运行asyncio代码模板.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-12-09 10:40
'''
import asyncio

import aiohttp
from bs4 import BeautifulSoup

url = "https://movie.douban.com/top250?start={}&filter="

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
                  "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
}

async def get_movie_info(page):
    async with aiohttp.ClientSession() as session:
        async with session.get(url.format(page * 25), headers=headers) as resp:
            soup = BeautifulSoup(await resp.text(), "lxml")
            div_list = soup.find_all("div", class_="hd")
            for title in div_list:
                print(title.get_text())



# 创建一个启动函数
async def main():
    tasks = [loop.create_task(get_movie_info(i)) for i in range(10)]
    await asyncio.wait(tasks)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())